package com.example.android.commitcontent.ime.activities;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.example.android.commitcontent.ime.R;
import com.example.android.commitcontent.ime.cores.base.BaseActivity;
import com.example.android.commitcontent.ime.cores.utils.PopupHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    public static ComponentName COMPONENT_NAME;

    @BindView(R.id.btn_check_ime)
    Button mBtnCheckIME;
    @BindView(R.id.btn_switch_ime)
    Button mBtnSwitchIME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        COMPONENT_NAME = getComponentName();
        hideTitleBackButton();
        setTitleBarText(getString(R.string.app_name));
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("mft", "当前已经安装的输入法有");
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        List<InputMethodInfo> methodList = imm.getInputMethodList();
        for (InputMethodInfo mi : methodList) {
            CharSequence name = mi.getPackageName();
            Log.d("mft", "PackageName:" + name);
        }

        Log.d("mft", "已经勾选的输入法有");
        String enable = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.ENABLED_INPUT_METHODS);
        Log.d("mft", enable.replace(":", "\n"));


        Log.d("mft", "当前默认输入法是");
        String currentInputmethod = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.DEFAULT_INPUT_METHOD);
        Log.d("mft", currentInputmethod);

    }

    @OnClick(R.id.btn_check_ime)
    void btn_check_ime() {
        //跳转到输入法勾选设置页面
        Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
        startActivity(intent);
    }

    @OnClick(R.id.btn_switch_ime)
    void btn_switch_ime() {
        //弹出已勾选的输入法并设置默认
        Object systemService = getSystemService(INPUT_METHOD_SERVICE);
        if (systemService != null)
            ((InputMethodManager) systemService).showInputMethodPicker();
    }

    @Override
    protected int setLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        overrideToolbarMoreButtonEvent(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> strings = new ArrayList<>();
                strings.add("设置");
                strings.add("意见反馈");
                strings.add("退出");
                PopupHelper.newInstance(MainActivity.this)
                        .setTitleText("更多选项")
                        .setPopupWidth(190, true)
                        .setDataList(strings, new PopupHelper.DataConverter<String>() {
                            @Override
                            public String convertToString(String data) {
                                return data;
                            }
                        })
                        .setItemListener(new PopupHelper.OnItemClickListener() {
                            @Override
                            public void onItemClick(PopupWindow popupWindow, int position) {
                                popupWindow.dismiss();
                                switch (position) {
                                    case 0:
                                        SettingActivity.start(MainActivity.this);
                                        break;
                                    case 1:
                                        FeedbackActivity.start(MainActivity.this);
                                        break;
                                    case 2:
                                        finish();
                                        break;
                                }
                            }
                        })
                        .showAsDropDown(view, -160, 0, true);
            }
        });
    }

    @Override
    protected void initData() {

    }
}
